/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Models.Dep;
import Models.Med;
import Models.Pays;
import Models.Spe;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uc = request.getParameter("uc");
        String dep = request.getParameter("Departement");
        String med = request.getParameter("nom");
        String spe = request.getParameter("specialitecomplementaire");
        String page;
        if (uc == null) {
            page = "menu.jsp";
        } else {
            switch (uc) {
                case "dep":
                    if (dep == null) {
                        Collection<Dep> d = p.getLesDeps();
                        request.setAttribute("lesDeps", d);
                        request.setAttribute("Pays", p);
                        page = "index.jsp";
                    } else {
                        page = "affimed.jsp";

                        Dep d = p.getLeDep(dep);
                        Collection<Med> m = d.lesMeds();
                        request.setAttribute("lesMeds", m);
                        request.setAttribute("Dep", d);

                    }
                    break;
                case "med":
                    String nom = request.getParameter("nom");
                    if (nom == null) {
                        page = "nom.jsp";
                    } else {
                        Collection<Med> maCol = new TreeSet<Med>();

                        for (Dep d : p.getLesDeps()) {
                            for (Med m : d.lesMeds()) {
                                if (m.getNom().startsWith(nom)) {
                                    maCol.add(m);
                                }
                            }
                        }
                        request.setAttribute("maCol", maCol);
                        page = "listeMed.jsp";
                        
                    }
                    break;
                case "spe":
                    if (spe == null) {
                        Collection<Spe> s = p.getLesSpes();
                        request.setAttribute("lesSpes", s);
                        request.setAttribute("Pays", p);
                        
                        page = "spec.jsp";
                    } else {
                        page = "affimed2.jsp";

                        Spe s = p.getLaSpe(spe);
                        Collection<Med> m = s.lesMeds();
                        request.setAttribute("lesMeds", m);
                        request.setAttribute("Spe", s);
                        
                        //request.setAttribute("Pays", p);
                        
                    }

                    break;
                default:
                    page = "menu.jsp";
                    break;
            }

        }

        request.getRequestDispatcher(page).forward(request, response);

        //Med m = p.getLeDep(dep).LesMeds();
    }

    private Pays p;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        p = new Pays();

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
